package de.hpi.generator.config;

import java.util.ArrayList;

public class DatabaseModel {

    /**
     * @uml.property  name="name"
     */
    public String name;
    /**
     * @uml.property  name="tables"
     * @uml.associationEnd  multiplicity="(0 -1)" elementType="de.hpi.generator.config.TableModel"
     */
    public ArrayList<TableModel> tables = new ArrayList<TableModel>();
    /**
     * @uml.property  name="inclusionDependencies"
     * @uml.associationEnd  multiplicity="(1 -1)" elementType="de.hpi.generator.config.InclusionDependencyModels"
     */
    public DependencyModels InclusionDependencies = new DependencyModels();

    public DatabaseModel(String name) {
        this.name = name;

    }

    public void addTableModel(TableModel tableModel) throws ExistsException {
        if (findTableModelByName(tableModel.name) == null) {
            this.tables.add(tableModel);
        } else {
            throw new ExistsException("The table " + tableModel.name + " is already exists");
        }
    }

    public TableModel findTableModelByName(String aTableModelName) {
        TableModel result = null;
        for (TableModel aTestObject : this.tables) {
            if (aTestObject.name.compareTo(aTableModelName) == 0) {
                result = aTestObject;
            }
        }
        return result;
    }

    @Override
    public String toString() {
        String result = String.format("{name: %s, \n TableModels:[ \n", this.name);
        String aTableModelsString = "";
        String aInclusionDependencyModelString = "";
        for (TableModel aTableModel : tables) {
            aTableModelsString += "\t" + aTableModel.toString();
        }
        for (DependencyModel aInclusionDependencyModel : InclusionDependencies) {
            aInclusionDependencyModelString += "\t" + aInclusionDependencyModel.toString();
        }
        return result + aTableModelsString + "] \n InclusionDependencies:[ \n" + aInclusionDependencyModelString + "]}\n";
    }
}
